package com.blyat.xsoft.kernel.restful.view;

import com.blyat.xsoft.kernel.exception.BaseException;
import com.google.common.base.Strings;

import java.io.Serializable;

/**
 * @author syh
 * @date 2019/8/27
 **/
public class ResultModel implements Serializable {
    private static final long serialVersionUID = 1L;
    public static final int SUCCESS_CODE = 200;
    public static final int ERROR_CODE = -1;
    private int code;
    private Object data;
    private String message;

    public ResultModel() {
        this.code = SUCCESS_CODE;
    }

    public ResultModel(int code, Object data, String message) {
        this.code = SUCCESS_CODE;
        this.code = code;
        this.data = data;
        this.message = message;
    }

    public static ResultModel SUCCESS(String message, Object data) {
        return new ResultModel(SUCCESS_CODE, data, message);
    }

    public static ResultModel SUCCESS(Object data) {
        return new ResultModel(SUCCESS_CODE, data, "");
    }

    public static ResultModel SUCCESS() {
        return new ResultModel(SUCCESS_CODE, (Object) null, "");
    }


    public static ResultModel FAIL(String message) {
        return new ResultModel(ERROR_CODE, null, message);
    }

    public static ResultModel FAIL(int code, String message) {
        return new ResultModel(code, null, message);
    }

    public static ResultModel FAIL(BaseException exception) {
        return FAIL(Integer.parseInt(exception.getResult()), exception.getMessage());
    }

    public int getCode() {
        return this.code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Object getData() {
        return null != this.data && !"null".equals(this.data) ? this.data : "";
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getMessage() {
        return Strings.isNullOrEmpty(this.message) ? "" : this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
